﻿using System.ComponentModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WebAppIdentity.HttpContexts;
using WebAppIdentity.Request;
using WebAppIdentity.Response;
using WebAppIdentity.Services;

namespace WebAppIdentity.Controllers
{
	/// <summary>
	/// 用户模块
	/// </summary>
	[ApiController]
	[Route("[controller]")]
	public class UserController : ControllerBase
	{
		private readonly IUserService _userService;
		private readonly IHttpContextWrapper _httpContextWrapper;


		public UserController(IUserService userService, IHttpContextWrapper httpContextWrapper)
		{
			_userService = userService;
			_httpContextWrapper = httpContextWrapper;
		}

		/// <summary>
		/// 注册
		/// </summary>
		/// <param name="request"></param>
		/// <returns></returns>
		[HttpPost("Register")]
		[AllowAnonymous]
		public async Task<IActionResult> Register(RegisterRequest request)
		{
			var result = await _userService.RegisterAsync(request.UserName, request.Password, request.Address);
			if (!result.Success)
			{
				return BadRequest(new FailedResponse()
				{
					Errors = result.Errors
				});
			}
			return Ok(new TokenResponse
			{
				AccessToken = result.AccessToken,
				TokenType = result.TokenType,
				RefreshToken = result.RefreshToken,
				ExpiresIn = result.ExpiresIn
			});
		}

		/// <summary>
		/// 登录
		/// </summary>
		/// <param name="request"></param>
		/// <returns></returns>
		[HttpPost("Login")]
		[AllowAnonymous]
		public async Task<IActionResult> Login(LoginRequest request)
		{
			var result = await _userService.LoginAsync(request.UserName, request.Password);
			if (!result.Success)
			{
				return Unauthorized(new FailedResponse()
				{
					Errors = result.Errors
				});
			}

			return Ok(new TokenResponse
			{
				AccessToken = result.AccessToken,
				TokenType = result.TokenType,
				RefreshToken = result.RefreshToken,
				ExpiresIn = result.ExpiresIn
			});
		}

		/// <summary>
		/// 修改密码
		/// </summary>
		/// <param name="modifyPasswordRequest"></param>
		/// <returns></returns>
		[HttpPost("ModifyPassword")]
		public async Task<IActionResult> ModifyPassword(ModifyPasswordRequest modifyPasswordRequest)
		{
			var token = _httpContextWrapper.GetToken();
			var result = await _userService.ModifyPassword(modifyPasswordRequest, token);
			if (!result.Success)
			{
				return BadRequest(new FailedResponse()
				{
					Errors = result.Errors
				});
			}
			return Ok();
		}

		/// <summary>
		/// 刷新token
		/// </summary>
		/// <param name="request"></param>
		/// <returns></returns>
		[HttpPost("RefreshToken")]
		[AllowAnonymous]
		public async Task<IActionResult> RefreshToken(RefreshTokenRequest request)
		{
			var result = await _userService.RefreshTokenAsync(request.AccessToken, request.RefreshToken);
			if (!result.Success)
			{
				return Unauthorized(new FailedResponse()
				{
					Errors = result.Errors
				});
			}

			return Ok(new TokenResponse
			{
				AccessToken = result.AccessToken,
				TokenType = result.TokenType,
				ExpiresIn = result.ExpiresIn,
				RefreshToken = result.RefreshToken
			});
		}
	}
}
